/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.stock.entity.Container;
import com.thinkgem.jeesite.modules.stock.entity.WarehouseLoc;
import com.thinkgem.jeesite.modules.stock.service.ContainerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.entity.Warehouse;
import com.thinkgem.jeesite.modules.stock.service.WarehouseService;

import java.util.List;

/**
 * 仓库Controller
 * @author 仓库
 * @version 2019-12-22
 */
@Controller
@RequestMapping(value = "${adminPath}/stock/warehouse")
public class WarehouseController extends BaseController {

	@Autowired
	private WarehouseService warehouseService;
	
	@ModelAttribute
	public Warehouse get(@RequestParam(required=false) String id) {
		Warehouse entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = warehouseService.get(id);
		}
		if (entity == null){
			entity = new Warehouse();
		}
		return entity;
	}
	
	@RequiresPermissions("stock:warehouse:view")
	@RequestMapping(value = {"list", ""})
	public String list(Warehouse warehouse, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Warehouse> page = warehouseService.findPage(new Page<Warehouse>(request, response), warehouse); 
		model.addAttribute("page", page);
		return "modules/stock/warehouseList";
	}

	@RequiresPermissions("stock:warehouse:view")
	@RequestMapping(value = "form")
	public String form(Warehouse warehouse, Model model) {
		model.addAttribute("warehouse", warehouse);
		return "modules/stock/warehouseForm";
	}

	@Autowired
	private ContainerService containerService;
//	@RequiresPermissions("stock:warehouse:edit")
//	@RequestMapping(value = "tDeliver")
//	public String tDeliver(String id,int gs,int rl,String lx, Model model, RedirectAttributes redirectAttributes) {
//		 Warehouse warehouse=warehouseService.get(id);
//		for (int i = 0; i < gs; i++) {
//			Container c=new Container();
//			c.setNumber(rl);
//			c.setC(warehouse);
//			c.setName(warehouse.getName()+"_"+i+1);
//			c.setCtype(lx);
//			c.setStatus("1");
//			containerService.save(c);
//		}
//		addMessage(redirectAttributes, "设置托盘成功");
//		return "redirect:"+Global.getAdminPath()+"/stock/warehouse/?repage";
//	}
	@RequiresPermissions("stock:warehouse:edit")
	@RequestMapping(value = "save")
	public String save(Warehouse warehouse, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, warehouse)){
			return form(warehouse, model);
		}
		warehouseService.save(warehouse);
		addMessage(redirectAttributes, "保存仓库成功");
		return "redirect:"+Global.getAdminPath()+"/stock/warehouse/?repage";
	}
	
	@RequiresPermissions("stock:warehouse:edit")
	@RequestMapping(value = "delete")
	public String delete(Warehouse warehouse, RedirectAttributes redirectAttributes) {
		warehouseService.delete(warehouse);
		addMessage(redirectAttributes, "删除仓库成功");
		return "redirect:"+Global.getAdminPath()+"/stock/warehouse/?repage";
	}
	@RequestMapping(value = "getcid")
	@ResponseBody
	public List<WarehouseLoc> getcid(Warehouse warehouse, RedirectAttributes redirectAttributes) {
		return warehouseService.get(warehouse.getId()).getWarehouseLocList();

	}
}